﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AioNightly.Model;
using AioNightly.Dao;
using AioNightly.Common;

namespace AioNightly
{
    public partial class New : System.Web.UI.Page
    {
        private string m_defaultStr = "Please Select";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckUser();
                GetProducts();
                StoreDefaultSelect();
                RefreshRepeater();
            }
        }

        private User m_user;
        private void CheckUser()
        {
            m_user = (User)Session["User"];

            if (m_user == null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }

        private void GetProducts()
        {
            ProductDAO m_productDAO = new ProductDAO();
            Product[] m_productArray = m_productDAO.GetAllProducts().ToArray();
            ddl_product.DataSource = m_productArray;
            ddl_product.DataTextField = "Name";
            ddl_product.DataBind();
        }

        private void StoreDefaultSelect()
        {
            if (ddl_product.Items.Count > 0)
            {
                if (ddl_product.Items[0].Value != m_defaultStr)
                {
                    ddl_product.Items.Insert(0, m_defaultStr);
                }
            }
            else
                ddl_product.Items.Insert(0, m_defaultStr);
        }

        protected void bt_addsummary_Click(object sender, EventArgs e)
        {
            AddNote((int)TypeManager.ItemType.Summary, tb_addnote.Text.Trim(), ddl_product.SelectedIndex);
            RefreshRepeater();
        }

        protected void bt_addbug_Click(object sender, EventArgs e)
        {
            AddNote((int)TypeManager.ItemType.Bug, tb_addnote.Text.Trim(), ddl_product.SelectedIndex);
            RefreshRepeater();
        }

        private void AddNote(int type, string note,int productId)
        {
            if (tb_addnote.Text.Trim().Length > 0)
            {
                NewnoteDAO m_newnoteDAO = new NewnoteDAO();
                m_newnoteDAO.AddNewnote(type, note, ddl_product.SelectedIndex);

                tb_addnote.Text = null;
            }
        }

        private void RefreshRepeater()
        {
            NewnoteDAO m_newnoteDAO = new NewnoteDAO();

            NewNote[] m_newnoteArray = m_newnoteDAO.GetNewnoteByProduct(ddl_product.SelectedValue).ToArray();

            rp_newitem.DataSource = m_newnoteArray;
            rp_newitem.DataBind();
        }

        protected void rp_newitem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((HyperLink)e.Item.FindControl("hl_note")).Text = string.Format("{0}. {1}", e.Item.ItemIndex + 1, ((NewNote)e.Item.DataItem).Content);
                ((HyperLink)e.Item.FindControl("hl_note")).NavigateUrl = string.Format("EditNew.aspx?new={0}", ((NewNote)e.Item.DataItem).Id);
                ((HyperLink)e.Item.FindControl("hl_note")).ForeColor = System.Drawing.Color.Black;
            }
        }

        protected void ddl_product_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshRepeater();
        }
    }
}